package com.heima.aliyun.oss;

import com.aliyun.oss.OSS;
import com.heima.aliyun.oss.config.OssProperties;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author Administrator
 */
public class AliyunOssTemplate {
    private OssProperties ossProperties;
    private OSS ossClient;

    public AliyunOssTemplate(OssProperties ossProperties, OSS ossClient) {
        this.ossProperties = ossProperties;
        this.ossClient = ossClient;
    }

    /**
     * 上传文件
     *
     * @param filename 文件名称
     * @param is       文件流
     * @return
     */
    public String upload(String filename, InputStream is) {
        String storePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date()) + "/" + UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
        ossClient.putObject(ossProperties.getBucketName(), storePath, is);
        return ossProperties.getReadPath() + storePath;
    }
}